package view.book;

import java.awt.*;

import java.awt.event.*;


import javax.swing.*;

import entity.Book;
import service.book.BookServer;
import service.book.BookServerImpl;
import view.MainView;
import view.CommonView.CommonJPanel;
import view.model.BookTableModel;

/**
 * @Author: Lin Hongwei
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 查询图书模块
 *
 */
public class QueryBookView extends CommonJPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private BookServer bookServer = new BookServerImpl();

    //创建表格
    private JTable table;
    private BookTableModel bookTableModel;

    //创建搜索按钮
    private JButton searchButton;

    //创建确认借阅按钮
    //    private JButton borrowButton;
    
    private JTextField bookNameField,authorField;

    //创建comboBox
	//    BookTypeComboBoxModel bcm = new BookTypeComboBoxModel();
	//    JComboBox<BookType> bookTypeField;
	//    BookTypeComboBoxModel bcm = new BookTypeComboBoxModel();
	//	  JComboBox bookTypeComboBox;





	public QueryBookView() {
		super(TYPE_TRANSPARENT);
		this.setSize(MainView.instance.getWidth(),MainView.instance.getHeight());
		
		//标签、输入框面板
        JPanel searchPanel = new CommonJPanel(CommonJPanel.TYPE_TRANSPARENT);
        searchPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 20));
        searchPanel.setPreferredSize(new Dimension(getWidth(), 70));
        add(searchPanel);

        //创建图书名称标签
        JLabel labelBookName = new JLabel("图书名称：");
        searchPanel.add(labelBookName);
        //创建图书名称输入框
        bookNameField = new JTextField();
        bookNameField.setPreferredSize(new Dimension(120, 30));
        searchPanel.add(bookNameField);

        //创建图书作者标签
        JLabel labelAuthor = new JLabel("图书作者：");
        searchPanel.add(labelAuthor);
        //创建图书作者输入框
        authorField = new JTextField();
        authorField.setPreferredSize(new Dimension(120, 30));
        searchPanel.add(authorField);

        //创建图书类别标签
		// JLabel labelBookType = new JLabel("图书类别：");
		// searchPanel.add(labelBookType);
        // 创建图书类别下拉框
		// bookTypeComboBox = new JComboBox<>(bcm);
		// bookTypeComboBox.setPreferredSize(new Dimension(120, 30));
		// bookTypeComboBox.setBackground(Color.white);
		//  searchPanel.add(bookTypeComboBox);

        //创建按钮
        searchButton = new JButton("查询");
        searchButton.setPreferredSize(new Dimension(100, 30));
        searchPanel.add(searchButton);


        //创建表格面板
		//        JPanel tablePanel = new JPanel();
		//        add(tablePanel);

        //创建表格
        bookTableModel = new BookTableModel();
        table = new JTable(bookTableModel);
        JScrollPane sp = new JScrollPane(table);
        sp.setPreferredSize(new Dimension(740, 500));
        sp.setBackground(new Color(246, 246, 246));
        add(sp);





        //添加搜索按钮事件
        addSearchListener();




	}
	

    /**
     * 添加搜索按钮事件
     */
    public void addSearchListener() {
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //更改表格的源数据
            	Book book = new Book();
        		book.setBookName(bookNameField.getText());
        		book.setBookAuthor(authorField.getText());
        		BookTableModel.cs = bookServer.getBookList(book);
        		table.updateUI();
            }
        });
    }

   
    /**
     * 更新数据
     */
    public void updateData() {
//        BookTableModel.cs = new BookService().list();
//        table.updateUI();
//        bcm.cs = new BookTypeService().list();
//        bookTypeField.updateUI();
    }

}
